import re

line = "Hello World!"

# H开头，任意字符，重复
pattern = "^H.*"
# H开头，任意字符，重复，以!号结尾
pattern2 = "^H.*!$"

result = re.match(pattern2, line)

if result:
    print(True)
else:
    print(False)


# 非贪婪匹配
line1 = "aabooooooobby"   # 提取booooooob
pattern3 = ".*?(b.*?b).*"
result1 = re.match(pattern3, line1)
print(result1.group(1)) # booooooob


# +号，代表最少出现一次
line2 = "aaboooobbbaabby"
pattern4 = ".*(b.+b).*"
result1 = re.match(pattern4, line2)
print(result1.group(1)) # baabb

# {2} {2,} {2,5}    限制字符出现的次数
# {2} 限制2次
# {2,} 至少2次
# {2,5} 至少2次，至多5次

line3 = "aaboooobbbaaaaabaaby"
pattern5 = ".*(b.{2}b).*"
result2 = re.match(pattern5, line3)
print(result2.group(1)) # baab

pattern6 = ".*(b.{3,}b).*"
result3 = re.match(pattern6, line3)
print(result3.group(1)) # baaaaabaab，至少3次

pattern7 = ".*(b.{3,4}b).*"
result4 = re.match(pattern7, line3)
print(result4.group(1)) # boooob，至少3次，至多4次


line4 = "bobby123"
pattern8 = "bobby|bobby123" # 匹配bobby或者bobby123